class Playlist < ApplicationRecord
  scope :ordered, -> { order(created_at: :desc, serial_id: :asc, id: :asc) }

  has_and_belongs_to_many :songs
  has_and_belongs_to_many :playlist_favs

  has_many :recommends, as: :entity

  def self.build_association
    begin
      self.ordered.each do |x|
        x.songs = []
        x.contain_songs.gsub("，", "/").split("/").each do |uuid|
          begin
            song = Song.find_by_uuid(uuid)
            x.songs << song
          rescue
            "什么都不做"
          end
        end
        x.save
      end
    rescue Exception => ex
      binding.pry
    end
  end
end
